#!/usr/bin/perl -w

=head1 NAME

 dust

 Copyright (C) 2007 Paul Schulz <paul@mawsonlakes.org>

=head1 SYNOPSIS

 Manipulating files and documents as 'dust'.

=cut

$option{'store_dir'}='cache/store';
$option{'refs_dir'}='cache/refs';

$command="";

if ( $#ARGV == -1 )
  {
    &usage();
    exit(0);
  }
elsif( $#ARGV == 0 )
  {
    $command = shift @ARGV;

    if ( $command eq "help" )
      {
	&usage();
	exit(0);
      }
  }
elsif( $#ARGV == 1 )
  {
    $command = shift @ARGV;

    if ( $command eq 'publish' )
      {
	$document = shift @ARGV;
      }
    elsif ( $command eq 'scan' )
      {
	$document = shift @ARGV;
      }
    elsif ( $command eq 'store' )
      {
	$document = shift @ARGV;
      }
  }
else
  {
    &usage();
    exit(0);
  }

sub usage ( )
  {
    printf( "usage dust <command> <arguments>\n" );
    printf( "  scan <file>    - Scan document structure\n" );
    printf( "  publish <file> - Publish document\n" );
    printf( "  store <file>   - Usage information\n" );
    printf( "  help           - Usage information\n" );
  }

my @links;
my %links;

sub file_hash ( $ )
  {
    my($filename)=@_;
    my $filehash;

    $filehash=`sha1sum -b $filename | awk '{ print \$1 }'`;
    chomp $filehash;

    return $filehash;
}

=head1 file_type

 Get the file mime type.

=cut

sub file_type ( $ )
  {
    my($filename)=@_;
    my($type);

    $type=`file -bi $filename`;
    chomp $type;

    return $type;
  }

=head1 parse_file

 Parse the named file are pull out any file links if the file format
 supports links.

=cut

sub parse_file ( $ $ )
  {
    my($path,$doc)=@_;
    my($filename)="$path/$doc";
    my @links;

    if( -e $filename )
      {
	$type = file_type($filename);

	if ( $type eq "text/html" )
	  {
	    open(IFILE,"$filename");
	    while($line=<IFILE>)
	      {
#		print OFILE $line;
		if ( $line =~ /href=\"([^\"]*)/ )
		  {
		    my $ref=$1;
		    push @links, $ref;
		  }
		elsif( $line =~ /src=\"([^\"]*)/ )
		  {
		    my $ref=$1;
		    push @links, $ref;
		  }
	      }
	    close(IFILE);
	  }
	else
	  {

	  }

      }
    return @links;
  }

=head1 dir_walk

 Portions of code taken, and modified from from Higher-Order Perl by
 Mark Dominus, published by Morgan Kaufmann Publishers, Copyright
 2005 by Elsevier Inc.

 Chapter 1 section 5

=cut

sub dir_walk ( $ $ )
  {
    my ($top, $code) = @_;
    my $DIR;

    $code->($top);

    if (-d $top)
      {
	my $file;
	unless (opendir $DIR, $top)
	  {
	    warn "Couldn't open directory $top: $!; skipping.\n";
	    return;
	  }
	while ($file = readdir $DIR)
	  {
	    next if $file eq '.' || $file eq '..';
	    &dir_walk("$top/$file", $code);
	  }
      }
  }

sub doc_walk ( $ $ $ )
  {
    my ($path,$doc,$code) = @_;
    my %doc;

    $code->($path,$doc);

    @links=parse_file($path,$doc);
    if ( $#links != -1 )
      {
	for $link (@links)
	  {
	    &doc_walk($path,$link,$code);
	  }
      }
    else
      {

      }
  }

sub print_doc ( $ )
  {
    print $_[0]."\n";
  }

sub print_hash_header ( )
  {
    $format="%-40s  %-20s  %s\n";
    printf($format,"Hash Identifier","Type","Name");
    printf($format,"---------------","----","----");
  }

sub print_hash ( $ )
  {
    my($path,$doc)=@_;
    my($document)="$path/$doc";
    printf("%s  %-20s  %s\n",
	   file_hash("$document"),
	   file_type("$document"),
	   $doc);
  }

$path=$document;
$path=~s/^(.*\/)[^\/]+$/$1/;
$doc=$document;
$doc=~s/^.*\/([^\/]+)$/$1/;

# print_hash_header;
# doc_walk( $path, $doc, \&print_hash );
# exit 1;

if ( $command eq 'publish' )
  {
    if ( -f $document )
      {
	$top = $document;
	$top =~ s/\/(.*)$//;
	$document = $1;
	parse_file( $top, $document );
      }
    else
      {
	printf( "*** Document not found: $document\n" );
	exit(0);
      }
  }
elsif ( $command eq 'store' )
  {
    if ( -f $document )
      {
	my ( $hash ) = file_hash($document);
	my ( $hash_path ) = $hash;
	$hash_path =~ s/^(..)/$1\//;

	printf( "File exists.\n" );
	printf( "Hash:".$hash."\n" );
	printf( "Path:".$hash_path."\n" );

	# Copy file to store
      }
  }

